# -*- python -*-
# ex: set syntax=python:

# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from buildbot.scheduler import Scheduler
from buildbot.schedulers import timed
from buildbot.status import mail

from common import chromium_utils

from master import gitiles_poller
from master import master_utils
from master import slaves_list
from master.factory import remote_run_factory

import config
import master_site_config

ActiveMaster = master_site_config.ChromiumSwarm

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}

# 'slavePortnum' defines the TCP port to listen on. This must match the value
# configured into the buildslaves (with their --master option)
c['slavePortnum'] = ActiveMaster.slave_port

config.DatabaseSetup(c)

####### CHANGESOURCES

# Polls config.Master.trunk_url for changes
master_poller = gitiles_poller.GitilesPoller(
    'https://chromium.googlesource.com/chromium/src')

c['change_source'] = [master_poller]

####### SCHEDULERS

s_chromium_swarm = Scheduler(name='experimental',
                             branch='master',
                             treeStableTimer=60,
                             builderNames=['Linux Swarm',
                                           'Android N5 Swarm',
                                           'Android N5X Swarm',
                                           'Mac Swarm'])

s_heartbeat = timed.Periodic(
    name='heartbeat', builderNames=['Heartbeat', 'Heartbeat Canary'],
    periodicBuildTimer=60)

c['schedulers'] = [s_chromium_swarm, s_heartbeat]


# ----------------------------------------------------------------------------
# BUILDER DEFINITIONS

def m_remote_run(recipe, **kwargs):
  return remote_run_factory.RemoteRunFactory(
      active_master=ActiveMaster,
      repository='https://chromium.googlesource.com/chromium/tools/build.git',
      recipe=recipe,
      **kwargs)

b_heartbeat = {
  'name': 'Heartbeat',
  'builddir': 'heartbeat',
  'factory': m_remote_run('swarming/heartbeat'),
  'auto_reboot': False,
  'category': 'swarming',
}

b_heartbeat_staging = {
  'name': 'Heartbeat Canary',
  'builddir': 'heartbeat_staging',
  'factory': m_remote_run(
      'swarming/heartbeat',
      factory_properties={'target_environment': 'staging'},
  ),
  'auto_reboot': False,
  'category': 'swarming',
}

b_chromium_linux_swarm = {
  'name': 'Linux Swarm',
  'factory': m_remote_run('swarming/staging'),
  'auto_reboot': False,
  'category': 'swarming',
}

b_chromium_android_n5_swarm = {
  'name': 'Android N5 Swarm',
  'factory': m_remote_run('swarming/staging'),
  'category': 'swarming',
}

b_chromium_android_n5x_swarm = {
  'name': 'Android N5X Swarm',
  'factory': m_remote_run('swarming/staging'),
  'category': 'swarming',
}

b_chromium_mac_swarm = {
  'name': 'Mac Swarm',
  'factory': m_remote_run('swarming/staging'),
  'category': 'swarming',
}

c['builders'] = [
  b_heartbeat,
  b_heartbeat_staging,
  b_chromium_linux_swarm,
  b_chromium_android_n5_swarm,
  b_chromium_android_n5x_swarm,
  b_chromium_mac_swarm,
]

# Associate the slaves to the builders. The configuration is in slaves.cfg.
slaves = slaves_list.SlavesList('slaves.cfg', 'ChromiumSwarm')
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])


####### BUILDSLAVES

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)


####### STATUS TARGETS

# Must come before AutoSetupMaster().
c['buildbotURL'] = ActiveMaster.buildbot_url

# Adds common status and tools to this master.
master_utils.AutoSetupMaster(c, ActiveMaster,
    public_html="../master.chromium/public_html",
    templates=['../master.chromium/templates'],
    tagComparator=master_poller.comparator,
    enable_http_status_push=ActiveMaster.is_production_host)


####### PROJECT IDENTITY

c['projectName'] = ActiveMaster.project_name
c['projectURL'] = config.Master.project_url
